<?php

function checkCust($config,$nohp){
	$db =  new db($config);
	$id=0;
	$result = $db->query("SELECT * FROM mn_customer WHERE no_hape = '".urlencode($nohp)."'");
	if ($result && $db->rowCount() > 0) {
		$id = $result[0]['idCustomer'];
	}
	return $id;
}

function getPoin($config,$id){
	$db =  new db($config);
	$poin=0;
	$result = $db->query("SELECT poin FROM mn_customer WHERE idCustomer = '".urlencode($id)."'");
	if ($result && $db->rowCount() > 0) {
		$poin = $result[0]['poin'];
	}
	return $poin;
}

function StatusName($status){
	$val = "";
	switch ($status){
		case 1 :
			$val = "<font color=\"red\">Akan produksi</font>";
			break;
		case 2 :
			$val = "<font color=\"orange\">Akan kirim</font>";
			break;
		case 3 :
			$val = "<font color=\"green\">Sudah kirim</font>";
			break;
		default : break;
	}
	return $val;
}

function StatusBayar($status){
	$val = "";
	switch ($status){
		case 0 :
			$val = "<font color=\"red\">Belum bayar</font>";
			break;
		case 1 :
			$val = "<font color=\"green\">Sudah bayar</font>";
			break;
		default : break;
	}
	return $val;
}

function getCrew($config,$id){
	$retval = '';
	$db =  new db($config);
	$result = $db->query("SELECT username FROM mn_crew WHERE idCrew='$id'");
	if ($result && $db->rowCount() > 0) {
		$retval = $result[0]['username'];
	}
	return $retval;
}

function controller_produksi($config) {
	if (isset($_SESSION['logged'])){
		if ((isset($config['ruri'][1])) && ($config['ruri'][1]=='embozz')){
			//kalau embozz
			if (isset($_POST['tambah'])){	//tambah post submit
				$nohp = $_POST['nohp'];
				$nama = $_POST['nama'];
				$qty = $_POST['qty'];
				$prov = $_POST['prov'];
				$kota = $_POST['kota'];
				$kodepos = $_POST['pobox'];
				$alamat = $_POST['lengkap'];
				$db =  new db($config);
				$id = checkCust($config,$nohp);
				if ($id>0){
					//get poin
					$poin = getPoin($config,$id) + (10*$qty);
					//update poin
					$que = "UPDATE mn_customer SET poin = $poin WHERE idCustomer = '$id'";
					echo "lolos 1a ";
					$db->exec($que);
				}else{
					//masukkan mn_customer
					$poin = 10*$qty;
					$que = "INSERT INTO mn_customer (no_hape, nama, prov, kabkot, alamat, kodepos, poin, twitter, fb, isReseller) VALUES ('$nohp', '$nama', '$prov', '$kota', '$alamat', '$kodepos', '$poin', '', '', '0')";
					echo "lolos 1b ";
					$db->exec($que);
					$id = $db->lastInsertId();
				}
				//masukkan mn_embozz
				$tgl = date('Y-m-d H:i:s', time());
				$embozz ="INSERT INTO `mn_embozz` (`idCust` ,`nama` ,`qty` ,`prov` ,`kabkot` ,`alamat` ,`kodepos` ,`tgl_pesan` ,`status` , `bayar`, `modifier`) VALUES ('$id', '$nama', '$qty', '$prov', '$kota', '$alamat', '$kodepos', '$tgl', '1', '0', '".$_SESSION['logged']['id']."');";
				echo $embozz;
				$db->exec($embozz);
				$lid = $db->lastInsertId();
				$_SESSION['addsucc'] = "Pesanan embozz a.n '$nama' sebanyak $qty buah berhasil ditambahkan dengan nomor pesanan #$lid";
				$ukey = "nbga3t";
				$pkey = "t3agbn";
				//$nope = $_SESSION['logged']['no_hape'];
				$nope = '085295986405';
				$pesan = "Pesanan embozz baru : #$lid, $qty buah.\nOleh : ".$_SESSION['logged']['uname'];
				if ($_SESSION['logged']['id']!='4'){	//kalau bukan aep
					//KIRIM-SMS
					helper::kirimSMS($ukey,$pkey,$nope,$pesan);
				}
				if ($_SESSION['logged']['id']!='3'){
					//KIRIM DM
					$twitter = new TwitterOAuth(
						$config['consumer_key'],
						$config['consumer_secret'],
						$config['oauth_token'],
						$config['oauth_token_secret']
					);
					$content = $twitter->get('account/verify_credentials');	//verify token
					$receiver = 'akhmadrijal';
					$resp = $twitter->post('direct_messages/new',array('text'=>$pesan,'screen_name'=>$receiver));
				}
				//
				header('Location: '.$config['home_url']."produksi/embozz");
			} else if ((isset($config['ruri'][2])) && (isset($config['ruri'][3])) && ($config['ruri'][3]=='bayar')){
				//ubah status bayar
				$id = $config['ruri'][2];
				$db =  new db($config);
				$que = "UPDATE mn_embozz SET bayar = '1' WHERE idEmbozz = '$id'";
				echo $que;
				$db->exec($que);
				header('Location: '.$config['home_url']."produksi/embozz");
			} else if ((isset($config['ruri'][2])) && (isset($config['ruri'][3])) && ($config['ruri'][3]=='bayar-jadi')){
				//ubah status bayar-jadi
				$id = $config['ruri'][2];
				$db =  new db($config);
				$que = "UPDATE mn_embozz SET bayar = '1', status = '2' WHERE idEmbozz = '$id'";
				echo $que;
				$db->exec($que);
				header('Location: '.$config['home_url']."produksi/embozz");
			} else if ((isset($config['ruri'][2])) && (isset($config['ruri'][3])) && ($config['ruri'][3]=='prod')){
				//ubah status prod
				$id = $config['ruri'][2];
				$db =  new db($config);
				$que = "UPDATE mn_embozz SET status = '2' WHERE idEmbozz = '$id'";
				echo $que;
				$db->exec($que);
				header('Location: '.$config['home_url']."produksi/embozz");
			} else if ((isset($config['ruri'][2])) && (isset($config['ruri'][3])) && ($config['ruri'][3]=='kirim')){
				//ubah status kirim
				$id = $config['ruri'][2];
				$db =  new db($config);
				$que = "UPDATE mn_embozz SET status = '3' WHERE idEmbozz = '$id'";	//update pesanan
				echo $que;
				$db->exec($que);
				//GET jumlah kuota
				$result = $db->query("SELECT * FROM mn_embozz WHERE idEmbozz = '$id'");
				$qty = $result[0]['qty'];
				//SET gudangmin
				$que = "UPDATE mn_gudang SET stok = stok - $qty WHERE produk = 'embozz'";
				//echo $que;
				$db->exec($que);
				header('Location: '.$config['home_url']."produksi/embozz");
			} else if ((isset($config['ruri'][2])) && (isset($config['ruri'][3])) && ($config['ruri'][3]=='batal')){
				//ubah status batal
				$id = $config['ruri'][2];
				$db =  new db($config);
				$que = "UPDATE mn_embozz SET status = '4' WHERE idEmbozz = '$id'";
				echo $que;
				$db->exec($que);
				header('Location: '.$config['home_url']."produksi/embozz");
			} else if ((isset($config['ruri'][2])) && (isset($config['ruri'][3])) && ($config['ruri'][2]=='id')){
				//ubah pesanan embozz dan tampilkan form
				$id = $config['ruri'][3];
				if (isset($_POST['edit'])){
					//edit
					$nama = $_POST['nama'];
					$qty = $_POST['qty'];
					$idc = $_POST['idc'];
					$qtyold = $_POST['qtyold'];
					$prov = $_POST['prov'];
					$kota = $_POST['kota'];
					$kodepos = $_POST['pobox'];
					$alamat = $_POST['lengkap'];
					$db =  new db($config);
					$que = "UPDATE mn_embozz SET nama='$nama', qty='$qty', prov='$prov', kabkot='$kota', alamat='$alamat', kodepos='$kodepos' WHERE idEmbozz = '$id'";
					echo $que;
					$db->exec($que);
					$poin = getPoin($config,$idc) + (10*$qty) - (10*$qtyold);
					//update poin
					$que = "UPDATE mn_customer SET poin = $poin WHERE idCustomer = '$idc'";
					echo $que;
					$db->exec($que);
					echo "<script type=\"text/javascript\">self.close();</script>";
				}else
					view_pembozz($config,array(),$id);
			} else if ((isset($config['ruri'][2])) && ($config['ruri'][2]=='cek')){
				//cek pesanan buat AJAX
				$qword = $_REQUEST['qword'];
				$db =  new db($config);
				$result = $db->query("SELECT * FROM mn_embozz WHERE status < 4 AND (nama LIKE '%".urlencode($qword)."%' OR idEmbozz LIKE '%".urlencode($qword)."%') ORDER BY status ASC LIMIT 0, 25");
				echo "<ul>";
				if ($result && $db->rowCount() > 0) {
					foreach ($result as $pesanan){
						echo "<li>";
							$tgl = date('d/m/Y', strtotime($pesanan['tgl_pesan']));
							echo "<div><h5><strong><a target=\"_blank\" href=\"".$config['home_url']."pelanggan/id/".$pesanan['idCust']."\">".$pesanan['nama']."</a> ($tgl)</strong></h5></div>";
							$id = $pesanan['idEmbozz'];
							echo "<div>Nomor Pesanan : <a target=\"_blank\" href=\"".$config['home_url']."produksi/embozz/id/$id\">#$id</a></div>";
							echo "<div>Oleh : ".getCrew($config,$pesanan['modifier'])."</div>";
							echo "<div>Jumlah pesanan : ".$pesanan['qty']." buah</div>";
							echo "<div>Alamat : \"".$pesanan['alamat']."\", ".$pesanan['kabkot'].", ".$pesanan['prov']." - ".$pesanan['kodepos']."</div>";
							echo "<div>Bayar : ".StatusBayar($pesanan['bayar'])."</div>";
							echo "<div>Status : ".StatusName($pesanan['status'])."</div>";
						echo "</li>";
					}
				}else{
					echo "<li class=\"warning alert\">Tidak ditemukan data pesanan</li>";
				}
				echo "</ul>";
			} else if ((isset($config['ruri'][2])) && ($config['ruri'][2]=='gudang')){
				//GUDANG EMBOZZ
				$modif = $_SESSION['logged']['id'];
				if (isset($_POST['gudangplus'])){
					$qty = ($_POST['qty']=='') ? 0 : $_POST['qty'];
					$db =  new db($config);
					$que = "UPDATE mn_gudang SET stok = stok + $qty, modifier = '$modif' WHERE produk = 'embozz'";
					//echo $que;
					$db->exec($que);
					$result = $db->query("SELECT stok FROM mn_gudang WHERE produk = 'embozz'");
					$sisa = $result[0]['stok'];
					$_SESSION['succgudang'] = "Sukses menambah $qty buah";
					header('Location: '.$config['home_url'].'produksi/embozz/gudang');
				}else if (isset($_POST['gudangminus'])){
					$qty = ($_POST['qty']=='') ? 0 : $_POST['qty'];
					$db =  new db($config);
					$que = "UPDATE mn_gudang SET stok = stok - $qty, modifier = '$modif' WHERE produk = 'embozz'";
					//echo $que;
					$db->exec($que);
					$result = $db->query("SELECT stok FROM mn_gudang WHERE produk = 'embozz'");
					$sisa = $result[0]['stok'];
					$_SESSION['succgudang'] = "Sukses mengambil $qty buah";
					header('Location: '.$config['home_url'].'produksi/embozz/gudang');
				}else
					view_gembozz($config,array());
			} else{
				view_embozz($config,array());
			}
		} else if ((isset($config['ruri'][1])) && ($config['ruri'][1]=='cust')){
			//get data customer
			$nohp = $_REQUEST['nohp'];
			$db =  new db($config);
			$result = $db->query("SELECT * FROM mn_customer WHERE no_hape = '".urlencode($nohp)."'");
			if ($result && $db->rowCount() > 0) {
				echo json_encode(array('code'  =>  1,
					'nama'  =>  $result[0]['nama'],
					'prov'  =>  $result[0]['prov'],
					'kabkot'  =>  $result[0]['kabkot'],
					'alamat'  =>  $result[0]['alamat'],
					'kodepos'  =>  $result[0]['kodepos']
					));
			}else{
				echo json_encode(array('code'  =>  -1
					));
			}
		} else
			header('Location: '.$config['home_url']);
	}else{
		header('Location: '.$config['home_url']);
	}
}

?>
